package com.duzilin.ehrecyclerview.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.duzilin.ehrecyclerview.R;
import com.duzilin.ehrecyclerview.Wrapper.LoadmoreWrapper;

public class LoadMoreActivity extends Activity {
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_more);
        recyclerView = findViewById(R.id.load_more_rv);
        adapter = new MyAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initData();
        final LoadmoreWrapper<String> wrapper = new LoadmoreWrapper<>(adapter);
        wrapper.setLoadMoreView(R.layout.layout_load_more);
        wrapper.setOnLoadMoreListener(new LoadmoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 1; i <=6 ; i++) {
                            adapter.addData("new item" + i);
                        }
                        wrapper.notifyDataSetChanged();
                    }
                } , 3000);
            }
        });
        recyclerView.setAdapter(wrapper);
    }

    private void initData() {
        for (int i = 0; i < 10; i++) {
            adapter.addData(i+"");
        }
    }
}
